﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Data;
using Newtonsoft.Json;
using System.Web.SessionState;

namespace AjaxLibrary
{
    public class buyer_GeneralProduct : IHttpHandler, IRequiresSessionState
    {
        #region IHttpHandler 成员 常规商品

        public bool IsReusable
        {
            get { return true; }
        }

        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/json";
            string nodeId = context.Request["node"];

            if (!string.IsNullOrEmpty(nodeId))
            {
                DataTable dt = new DataTable();
                String[] ids = nodeId.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);

                int companyId = 0;
                companyId = logic.customer.getCompanyId();

                bool companyTypes = false;
                companyTypes = logic.company.listCompanyWeekWithByCompanyId(companyId); //企业类型（周订单企业）
                // 查询商品类别
                if (ids[0] == "0")
                {
                    int category = 0;
                    int.TryParse(ids[1], out category);

                    if (category != 0)
                    {
                        if (companyTypes)
                        {
                            dt = logic.buyerFixedPricePeriodItem.subCategorylistWithCompanyId(companyId, category);
                        }
                        else
                        {
                            //dt = logic.category.getByParent(category);
                            dt = logic.buyerRange.subCategory_listWithCompanyRange(companyId, category);
                        }
                    }
                    else
                    {
                        if (companyTypes)
                        {
                            dt = logic.buyerFixedPricePeriodItem.rootCategorylistWithCompanyId(companyId);
                        }
                        else
                        {
                            //dt = logic.category.listWithCompanyRange(companyId);
                            dt = logic.buyerRange.rootCategorylistWithCompanyRange(companyId);
                        }
                    }

                    using (JsonTextWriter writer = new JsonTextWriter(context.Response.Output))
                    {
                        writer.WriteStartArray();

                        foreach (DataRow row in dt.Rows)
                        {
                            writer.WriteStartObject();
                            writer.WritePropertyName("id");
                            if (row["treelevel"].ToString() == "1" && row["parentId"].ToString() != "0")
                            {
                                writer.WriteValue("1," + row["categoryId"]);
                            }
                            else
                            {
                                writer.WriteValue("0," + row["categoryId"]);
                            }
                            writer.WritePropertyName("text");
                            writer.WriteValue(row["name"]);
                            writer.WritePropertyName("singleClickExpand");
                            writer.WriteValue(true);
                            writer.WriteEndObject();
                        }

                        writer.WriteEndArray();
                    }
                }
                else
                {
                    // 查询商品
                    if (companyTypes)
                    {
                        dt = logic.buyerFixedPricePeriodItem.listWithCategoryAndCompany(companyId, Convert.ToInt32(ids[1]));
                    }
                    else
                    {
                        //dt = logic.product.getByCategoryLimited(Convert.ToInt32(ids[1]), logic.customer.getCompanyId());
                        dt = logic.buyerRange.listWithCategoryAndCompany(companyId, Convert.ToInt32(ids[1]));
                    }
                    using (JsonTextWriter writer = new JsonTextWriter(context.Response.Output))
                    {
                        writer.WriteStartArray();

                        foreach (DataRow row in dt.Rows)
                        {
                            string names = row["name"] + "<font color='red'>" + (Convert.ToInt32(row["isCurrentCost"]) == 1 ? "时价" : String.Format("￥{0:F2}", row["newPrice"])) + "</font>";
                            writer.WriteStartObject();
                            writer.WritePropertyName("id");
                            writer.WriteValue("1" + row["id"]);
                            writer.WritePropertyName("text");
                            writer.WriteValue(names);
                            writer.WritePropertyName("leaf");
                            writer.WriteValue(true);
                            if (companyTypes)
                            {
                                writer.WritePropertyName("href");
                                writer.WriteValue(String.Format("javascript:callbackFunctionFixed({0},{1},{2});", row["productId"], row["businessNature"], row["fpid"]));
                            }
                            else
                            {
                                writer.WritePropertyName("href");
                                writer.WriteValue(String.Format("javascript:callbackFunction({0},{1});", row["productId"], row["businessNature"]));
                            }

                            writer.WritePropertyName("qtip");
                            writer.WriteValue(names);
                            writer.WriteEndObject();
                        }
                        writer.WriteEndArray();
                    }
                }
            }
        }

        #endregion
    }
}
